﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.DAL.RecordCenter;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;


namespace MS.InformationManagement.DAL.DTOs
{
    public static class RecordsSiteDTO
    {
        public static IEnumerable<Seccion> GetCollection(SPListItemCollection items)
        {
            List<Seccion> results = new List<Seccion>(); 
            var itemsList = from SPListItem item in items select item;
            foreach (SPListItem item in itemsList)
            {
                results.Add(new Seccion 
                {
                    Código = item["Code"].ToString(),
                    Title = item[SPBuiltInFieldId.Title].ToString(),
                    Id = item.ID,
                    Grupo = new SPFieldUserValue(SPContext.Current.Web, item[CustomSiteColumns.FIELD_SECURITY_GROUP].ToString()).LookupValue,
                    GrupoId = new SPFieldUserValue(SPContext.Current.Web, item[CustomSiteColumns.FIELD_SECURITY_GROUP].ToString()).LookupId,
                    UsuarioEncargadoCorrespondencia = new SPFieldUserValue(SPContext.Current.Web, item[CustomSiteColumns.FIELD_USER_IN_CHARGE].ToString()).LookupValue,
                    UsuarioEncargadoCorrespondenciaId = new SPFieldUserValue(SPContext.Current.Web, item[CustomSiteColumns.FIELD_SECURITY_GROUP].ToString()).LookupId,
                    SecciónPadre = new SeccionesSeccion() { Id = item[CustomSiteColumns.FIELD_SECCION_PADRE_INTERNAL_NAME] == null ? 0 : int.Parse(item[CustomSiteColumns.FIELD_SECCION_PADRE_INTERNAL_NAME].ToString().Split(';')[0]) }
                });
            }
            //var results = from item in items.OfType<SPListItem>()
            //              select new Seccion
            //              {
            //                  Código = item["Code"].ToString(),
            //                  Title = item[SPBuiltInFieldId.Title].ToString(),
            //                  Id = item.ID,
            //                  Grupo = new SPFieldUserValue(SPContext.Current.Web, item[CustomSiteColumns.FIELD_SECURITY_GROUP].ToString()).LookupValue,
            //                  GrupoId = new SPFieldUserValue(SPContext.Current.Web, item[CustomSiteColumns.FIELD_SECURITY_GROUP].ToString()).LookupId,
            //                  UsuarioEncargadoCorrespondencia = new SPFieldUserValue(SPContext.Current.Web, item[CustomSiteColumns.FIELD_USER_IN_CHARGE].ToString()).LookupValue,
            //                  UsuarioEncargadoCorrespondenciaId = new SPFieldUserValue(SPContext.Current.Web, item[CustomSiteColumns.FIELD_SECURITY_GROUP].ToString()).LookupId,
            //                  SecciónPadre = new SeccionesSeccion() { Id = item[CustomSiteColumns.FIELD_SECCION_PADRE_INTERNAL_NAME] == null ? 0 : int.Parse(item[CustomSiteColumns.FIELD_SECCION_PADRE_INTERNAL_NAME].ToString().Split(';')[0]) }
            //              };
            return results.ToList();
        }

        public static IEnumerable<Serie> GetCollectionSeries(SPListItemCollection items)
        {
            List<Serie> results = new List<Serie>(); 
            var itemsList = from SPListItem item in items select item;
            foreach (SPListItem item in itemsList)
            {
                results.Add(new Serie 
                {
                    Código = item["Code"].ToString(),
                    Title = item[SPBuiltInFieldId.Title].ToString(),
                    Id = item.ID
                });
            }
            //var results = from item in items.OfType<SPListItem>()
            //              select new Serie
            //              {
            //                  Código = item["Code"].ToString(),
            //                  Title = item[SPBuiltInFieldId.Title].ToString(),
            //                  Id = item.ID
            //              };
            return results;
        }

        public static IEnumerable<Subseccion> GetCollectionSubSeccion(SPListItemCollection items)
        {
            List<Subseccion> results = new List<Subseccion>();
            var itemsList = from SPListItem item in items select item;
            foreach (SPListItem item in itemsList)
            {
                results.Add(new Subseccion
                {
                    Sección = getSeccion(item),
                    Title = item[SPBuiltInFieldId.Title].ToString(),
                    Id = item.ID
                });
            }
            //var results = from item in items.OfType<SPListItem>()
            //              select new Subseccion
            //              {
            //                  Sección = getSeccion(item),
            //                  Title = item[SPBuiltInFieldId.Title].ToString(),
            //                  Id = item.ID
            //              };
            return results;
        }

        public static IEnumerable<Subserie> GetCollectionSubSerie(SPListItemCollection items)
        {
            List<Subserie> results = new List<Subserie>();
            var itemsList = from SPListItem item in items select item;
            foreach (SPListItem item in itemsList)
            {
                results.Add(new Subserie
                {
                    Serie = getSerie(item),
                    Title = item[SPBuiltInFieldId.Title].ToString(),
                    Código = item["Code"] == null ? null : item["Code"].ToString(),
                    Id = item.ID,
                    TiempoDeRespuesta = item[CustomSiteColumns.FIELD_DOCUMENT_RESPONSETIME] == null ? new Nullable<double>() : Convert.ToDouble(item[CustomSiteColumns.FIELD_DOCUMENT_RESPONSETIME])
                });
            }
            //var results = from item in items.OfType<SPListItem>()
            //              select new Subserie
            //              {
            //                  Serie = getSerie(item),
            //                  Title = item[SPBuiltInFieldId.Title].ToString(),
            //                  Código = item["Code"] == null ? null : item["Code"].ToString(),
            //                  Id = item.ID,
            //                  TiempoDeRespuesta = item[CustomSiteColumns.FIELD_DOCUMENT_RESPONSETIME] == null ? new Nullable<double>() : Convert.ToDouble(item[CustomSiteColumns.FIELD_DOCUMENT_RESPONSETIME])
            //              };
            return results;
        }

        public static IEnumerable<ItemTRD> GetCollectionItemTRD(SPListItemCollection items)
        {
            List<ItemTRD> results = new List<ItemTRD>();
            var itemsList = from SPListItem item in items select item;
            foreach (SPListItem item in itemsList)
            {
                results.Add(new ItemTRD
                {
                    Id = item.ID,
                    TiposDocumentales = GetTiposDocumentales(item)
                });
            }
            //var results = from item in items.OfType<SPListItem>()
            //              select new ItemTRD
            //              {
            //                  Id = item.ID,
            //                  TiposDocumentales = GetTiposDocumentales(item)
            //              };
            return results;
        }

        public static IEnumerable<TipoDocumental> GetCollectionTipoDocumental(SPListItemCollection items)
        {
            List<TipoDocumental> results = new List<TipoDocumental>();
            var itemsList = from SPListItem item in items select item;
            foreach (SPListItem item in itemsList)
            {
                results.Add(new TipoDocumental
                {
                    Id = item.ID,
                    Código = item[CustomSiteColumns.FIELD_CODE] == null ? null : item[CustomSiteColumns.FIELD_CODE].ToString(),
                    FechaDeCreación = item[CustomSiteColumns.FIELD_TEMPLATE_CREATION_DATE] == null ? new Nullable<DateTime>()
                      : Convert.ToDateTime(item[CustomSiteColumns.FIELD_TEMPLATE_CREATION_DATE]),
                    Path = item[SPBuiltInFieldId.ServerUrl] == null ? null : item[SPBuiltInFieldId.ServerUrl].ToString(),
                    Title = item[SPBuiltInFieldId.Title] == null ? null : item[SPBuiltInFieldId.Title].ToString(),
                    Name = item.File.Name,
                    Versión = item[CustomSiteColumns.FIELD_VERSION] == null ? new Nullable<double>() : Convert.ToDouble(item[CustomSiteColumns.FIELD_VERSION])
                });
            }

            //var results = from item in items.OfType<SPListItem>()
            //              select new TipoDocumental
            //              {
            //                  Id = item.ID,
            //                  Código = item[CustomSiteColumns.FIELD_CODE] == null ? null : item[CustomSiteColumns.FIELD_CODE].ToString(),
            //                  FechaDeCreación = item[CustomSiteColumns.FIELD_TEMPLATE_CREATION_DATE] == null ? new Nullable<DateTime>()
            //                    : Convert.ToDateTime(item[CustomSiteColumns.FIELD_TEMPLATE_CREATION_DATE]),
            //                  Path = item[SPBuiltInFieldId.ServerUrl] == null ? null : item[SPBuiltInFieldId.ServerUrl].ToString(),
            //                  Title = item[SPBuiltInFieldId.Title] == null ? null : item[SPBuiltInFieldId.Title].ToString(),
            //                  Name = item.File.Name,
            //                  Versión = item[CustomSiteColumns.FIELD_VERSION] == null ? new Nullable<double>() : Convert.ToDouble(item[CustomSiteColumns.FIELD_VERSION])
            //              };
            return results;
        }

        public static Subserie GetSubSerie(SPListItem item)
        {

            var results = new Subserie
                          {
                              Serie = getSerie(item),
                              Title = item[SPBuiltInFieldId.Title].ToString(),
                              Código = item["Code"] == null ? null : item["Code"].ToString(),
                              Id = item.ID,
                              TiempoDeRespuesta = item[CustomSiteColumns.FIELD_DOCUMENT_RESPONSETIME] == null ? new Nullable<double>() : Convert.ToDouble(item[CustomSiteColumns.FIELD_DOCUMENT_RESPONSETIME])
                          };
            return results;
        }

        public static Subseccion GetSubSection(SPListItem item)
        {

            var results = new Subseccion()
                          {
                              Sección = getSeccion(item),
                              Title = item[SPBuiltInFieldId.Title].ToString(),
                              Código = item["Code"] == null ? null : item["Code"].ToString(),
                              Id = item.ID
                          };
            return results;
        }

        public static Seccion GetSeccion(SPListItem item)
        {
            SPFieldLookupValue seccionPadre = item[CustomSiteColumns.FIELD_SECCION_PADRE_INTERNAL_NAME] == null ? null : new SPFieldLookupValue(item[CustomSiteColumns.FIELD_SECCION_PADRE_INTERNAL_NAME].ToString());
            var results = new Seccion
                          {
                              Código = item["Code"].ToString(),
                              Title = item[SPBuiltInFieldId.Title].ToString(),
                              Id = item.ID,
                              Grupo = new SPFieldUserValue(item.Web, item[CustomSiteColumns.FIELD_SECURITY_GROUP].ToString()).LookupValue,
                              GrupoId = new SPFieldUserValue(item.Web, item[CustomSiteColumns.FIELD_SECURITY_GROUP].ToString()).LookupId,
                              UsuarioEncargadoCorrespondencia = new SPFieldUserValue(item.Web, item[CustomSiteColumns.FIELD_USER_IN_CHARGE].ToString()).LookupValue,
                              UsuarioEncargadoCorrespondenciaId = new SPFieldUserValue(item.Web, item[CustomSiteColumns.FIELD_SECURITY_GROUP].ToString()).LookupId,
                              SecciónPadre = seccionPadre == null ? null : new SeccionesSeccion() { Id = seccionPadre.LookupId, Title = seccionPadre.LookupValue }
                          };
            return results;
        }

        public static TipoDocumental GetTipoDocumental(SPListItem item)
        {
            var results = new TipoDocumental {
                Title = item.Title,
                FechaDeCreación = item[CustomSiteColumns.FIELD_TEMPLATE_CREATION_DATE] == null ? new Nullable<DateTime>() : Convert.ToDateTime(item[CustomSiteColumns.FIELD_TEMPLATE_CREATION_DATE]),
                Versión = item[CustomSiteColumns.FIELD_VERSION] == null? new Nullable<double>(): Convert.ToDouble(item[CustomSiteColumns.FIELD_VERSION]),
                Código = item[CustomSiteColumns.FIELD_CODE] == null? string.Empty: item[CustomSiteColumns.FIELD_CODE].ToString()
            };
            return results;
        }

        public static Serie GetSerieByItem(SPListItem item)
        {
            var results = new Serie
            {
                Title = item[SPBuiltInFieldId.Title].ToString(),
                Código = item["Code"] == null ? string.Empty : item["Code"].ToString(),
                Id = item.ID
            };
            return results;
        }

        private static Seccion getSeccion(SPListItem item)
        {
            SPFieldLookupValue seccion = new SPFieldLookupValue(item[CustomSiteColumns.FIELD_SECTION].ToString());
            return new Seccion() { Id = seccion.LookupId, Title = seccion.LookupValue };
        }

        private static Serie getSerie(SPListItem item)
        {
            SPFieldLookupValue serie = new SPFieldLookupValue(item[CustomSiteColumns.FIELD_SERIE].ToString());
            return new Serie() { Id = serie.LookupId, Title = serie.LookupValue };
        }

        private static Microsoft.SharePoint.Linq.EntitySet<TipoDocumental> GetTiposDocumentales(SPListItem item)
        {
            //Microsoft.SharePoint.Linq.EntitySet<TipoDocumental> tiposDocumentales = new Microsoft.SharePoint.Linq.EntitySet<TipoDocumental>();

            //SPFieldLookupValueCollection tipos = new SPFieldLookupValueCollection(item[CustomSiteColumns.FIELD_TIPOS_DOCUMENTALES].ToString());
            //var result = from lookupValue in tipos
            //             select new TipoDocumental { Id = lookupValue.LookupId, Title = lookupValue.LookupValue };
            //foreach (TipoDocumental tipoDocumental in result) { tiposDocumentales.Add(tipoDocumental); }
            //return tiposDocumentales;

            Microsoft.SharePoint.Linq.EntitySet<TipoDocumental> tiposDocumentales = new Microsoft.SharePoint.Linq.EntitySet<TipoDocumental>();

            SPFieldLookupValueCollection tipos = new SPFieldLookupValueCollection(item[CustomSiteColumns.FIELD_TIPOS_DOCUMENTALES].ToString());
            foreach (SPFieldLookupValue lookupValue in tipos)
            {
                TipoDocumental td = new TipoDocumental();
                td.Id = lookupValue.LookupId;
                td.Title = lookupValue.LookupValue;
                tiposDocumentales.Add(td);
            }
            return tiposDocumentales;


        }

    }
}
